<template>
  <div v-if="visible">
    <el-dialog :visible.sync="visible"
               title="同步权限"
               :modal="false"
               class="user-dialog add-or-updata"
               width="35%"
               :before-close="closeDialog"
               :close-on-click-modal="false"
               :close-on-press-escape="false">
      <el-form :model="dataForm"
               class="label-left-form"
               v-loading="dataListLoading"
               :element-loading-text="$t('loading')"
               ref="dataForm"
               label-position="left"
               @keyup.enter.native="dataFormSubmitHandle()"
               label-width="80px">
        <el-form-item prop="username"
                      label="选择用户"
                      :rules="stringRule(false)">
          <el-input v-model.trim="dataForm.username"
                    class="width-100"
                    :readonly="true"
                    @focus="focusUser"
                    placeholder="选择负责人"></el-input>
        </el-form-item>
      </el-form>
      <template slot="footer">
        <el-button @click="closeDialog()">{{ $t('cancel')}}</el-button>
        <el-button type="primary"
                   :loading="dataListLoading"
                   @click="dataFormSubmitHandle()">{{ $t('confirm')}}</el-button>
      </template>
    </el-dialog>
    <user-dialog ref="userDialog"
                 @callBack="userDialogCallBack" />
  </div>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import minFormValidate from '@/mixins/form-validate'
import userDialog from '@/components/user-dialog'
import debounce from 'lodash/debounce'
export default {
  mixins: [mixinViewModule, minFormValidate],
  data () {
    return {
      mixinViewModuleOptions: {
        getDeptListURL: '/system/sysUser/getDeptInUser',
        getRuleListURL: '/system/sysUser/getInitPermission', // 根据用户id获取对应的菜单树结构
        getDataListURL: '', // 数据列表接口，API地址
        getDataListIsPage: true, // 数据列表接口，是否需要分页？
        addOrUpdateURL: '', // 添加或修改接口，API地址
        getDataInfoURL: '', // 数据详情，API地址
        deleteURL: '', // 删除接口，API地址
        deleteIsBatch: true, // 删除接口，是否需要批量？
        exportURL: '' // 导出接口，API地址
      },
      userOptions: [],
      visible: false,
      dataForm: {
        userId: ''
      }
    }
  },
  components: {
    userDialog
  },
  methods: {
    // 打开用户弹窗
    focusUser () {
      // if (this.dataForm.pid === '') {
      //   return this.$message.warning('请选择部门')
      // }
      this.openDialog('userDialog', { deptId: this.dataForm.pid })
    },
    closeDialog () {
      this.deptListVisible = false
      this.visible = false
      this.$refs.dataForm.resetFields()
    },
    init (data) {
      this.userOptions = []
      this.deptList = []
      this.visible = true
    },
    // 用户弹窗回调
    userDialogCallBack (data) {
      this.dataForm = Object.assign({}, this.dataForm, {
        username: data.realName,
        userId: data.id
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.getDataList({
            url: this.mixinViewModuleOptions.getRuleListURL,
            userId: this.dataForm.userId
          }).then(res => {
            if (res.code !== 0) {
              return
            }
            this.$emit('callBack', [...res.data])
            this.closeDialog()
          })
        }
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>

<style lang="scss" scoped>

</style>
